/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.overview;

import java.awt.BorderLayout;
import java.util.Iterator;

import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import twaver.Alarm;

public class AlarmUIListPane extends JPanel {
    BorderLayout layout = new BorderLayout();
    JPanel collelatedAlarmPane = new JPanel();
    JPanel commentsPane = new JPanel();
    BorderLayout collelatedAlarmPaneLayout = new BorderLayout();
    JScrollPane collelatedAlarmScroll = new JScrollPane();
    JList collelatedAlarmList = new JList();
    BorderLayout commentsPaneLayout = new BorderLayout();
    JScrollPane commentsScroll = new JScrollPane();
    JTextArea txtComments = new JTextArea();
    JPanel clientPropertyPane = new JPanel();
    ClientPropertyTable clientPropertyTable = new ClientPropertyTable();
    JScrollPane clientPropertyScroll = new JScrollPane();
    BorderLayout clientPropertyPaneLayout = new BorderLayout();
    JTabbedPane tab = new JTabbedPane();

    public AlarmUIListPane() {
        try {
            jbInit();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    void jbInit() throws Exception {
        this.setLayout(layout);
        collelatedAlarmPane.setLayout(collelatedAlarmPaneLayout);
        commentsPane.setLayout(commentsPaneLayout);
        clientPropertyPane.setLayout(clientPropertyPaneLayout);
        collelatedAlarmPane.add(collelatedAlarmScroll, BorderLayout.CENTER);
        tab.add(commentsPane, "Comments");
        collelatedAlarmScroll.getViewport().add(collelatedAlarmList, null);
        this.add(tab, null);
        tab.add(collelatedAlarmPane, "Collelated alarms");
        commentsPane.add(commentsScroll, BorderLayout.CENTER);
        tab.add(clientPropertyPane, "Client properties");
        commentsScroll.getViewport().add(txtComments, null);
        clientPropertyPane.add(clientPropertyScroll, BorderLayout.CENTER);
        clientPropertyScroll.getViewport().add(clientPropertyTable, null);
    }

    public void reset() {
        this.txtComments.setText("");
        this.clientPropertyTable.setClientProperties(null);
    }

    public void setAlarm(Alarm alarm) {
        reset();
        if (alarm != null) {
            this.clientPropertyTable.setClientProperties(alarm.getClientProperties());
        }
        if (alarm.getComments() != null) {
            Iterator it = alarm.getComments().iterator();
            while (it.hasNext()) {
                Object comment = it.next();
                if (this.txtComments.getText() == null ||
                    this.txtComments.getText().equals("")) {
                    //if first comment.
                    this.txtComments.setText(comment.toString());
                } else {
                    //if following comment.
                    this.txtComments.setText(this.txtComments.getText() +
                                             "\n" +
                                             "-------\n" +
                                             comment.toString());
                }
            }
        }
    }
}